package com.sc.util.openAPI;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.sc.dal.dto.order.TCustomer;
import com.sc.dal.dto.order.TInventory;
import com.sc.dal.dto.order.TQueryExecutingDto;
import com.sc.dal.dto.order.TSaleDeliverResult;
import com.sc.dal.dto.order.TSaleOrderBatchInsertResult;
import com.sc.dal.dto.order.TSaleOrderDetails;
import com.sc.dal.dto.order.TSaleOrderDto;
import com.sc.dal.dto.order.TSaleOrderInsertDto;
import com.sc.dal.dto.order.TSaleOrderInsertResult;
import com.sc.dal.dto.order.TSaleOrderResult;
import com.sc.dal.dto.order.TSearchItem;
import com.sc.dal.dto.order.TUnit;
import com.sc.util.adapter.ConfigBeanAdapter;
import com.sc.util.data.Common;


//T+数据请求工具类
public class TOpenAPIUtil {
	/**
	 * @Title: getTSaleOrderByExternalCode
	 * @Description 根据企商云订单号查询T+系统查询该销售订单
	 * @param externalCode 企商云订单号
	 * @date 2017年4月17日下午14:01:35
	 * @author LeiJia  
	 * @return TSaleOrderResult   返回类型
	 */
	public static TSaleOrderResult getTSaleOrderByExternalCode(String externalCode,boolean isOneInsert)throws Exception{
		//查询订单执行情况
		TQueryExecutingDto dto = new TQueryExecutingDto();
		List<TSearchItem> items = new ArrayList<TSearchItem>();
		TSearchItem item = new TSearchItem();
		item.setWhereName("SaleOrder.ExternalCode");
		item.setBeginValue(externalCode);
		items.add(item);
		dto.setQueryParam(items);
		String data = JSON.toJSONString(dto);
		System.out.println(data);
		OpenAPI api = new OpenAPI(ConfigBeanAdapter.t_srvUrl,ConfigBeanAdapter.t_appKey,ConfigBeanAdapter.t_appSecret,ConfigBeanAdapter.t_user,ConfigBeanAdapter.t_pwd,ConfigBeanAdapter.t_account);	 
		if(isOneInsert){
			System.out.println("开始登录！");
			api.getTLogin();
		}
		String r = api.getData("saleOrder/QueryExecuting",data);
		if(Common.isNotEmpty(r)){
			TSaleOrderResult result = JSON.parseObject(r, TSaleOrderResult.class);
			System.out.println(result);
			System.out.println(r);
			return result;
		}
		if(isOneInsert){
			System.out.println("正在退出！");
			api.Logout(api.Access_Token);
		}
		return null;
	}

	/**
	 * @Title: getTSaleOrderByExternalCode
	 * @Description 根据企商云订单号查询T+系统查询销售订单
	 * @param externalCode 企商云订单号
	 * @date 2017年4月17日下午14:01:35
	 * @author LeiJia  
	 * @return TSaleOrderResult  返回类型
	 */
	public static TSaleOrderResult getTSaleOrderByTSaleCode(String saleCode ,boolean isOneInsert)throws Exception{
		//查询订单执行情况
		TQueryExecutingDto dto = new TQueryExecutingDto();
		List<TSearchItem> items = new ArrayList<TSearchItem>();
		TSearchItem item = new TSearchItem();
		item.setWhereName("SaleOrder.Code");
		item.setBeginValue(saleCode);
		items.add(item);
		dto.setQueryParam(items);
		String data = JSON.toJSONString(dto);
		System.out.println(data);
		OpenAPI api = new OpenAPI(ConfigBeanAdapter.t_srvUrl,ConfigBeanAdapter.t_appKey,ConfigBeanAdapter.t_appSecret,ConfigBeanAdapter.t_user,ConfigBeanAdapter.t_pwd,ConfigBeanAdapter.t_account);
		if(isOneInsert){
			System.out.println("开始登录！");
			api.getTLogin();
		}
		String r = api.getData("saleOrder/QueryExecuting",data);
		if(Common.isNotEmpty(r)){
			TSaleOrderResult result = JSON.parseObject(r, TSaleOrderResult.class);
			System.out.println(result);
			System.out.println(r);
			return result;
		}
		if(isOneInsert){
			System.out.println("正在退出！");
			api.Logout(api.Access_Token);
		}
		return null;
	}
	
	/**
	 * @Title: getTSaleOrderByExternalCode
	 * @Description 根据T+销货单号查询T+销货单执行情况
	 * @param externalCode 企商云订单号
	 * @date 2017年4月17日下午14:01:35
	 * @author LeiJia  
	 * @return TSaleDeliverResult   返回类型
	 */
	public static TSaleDeliverResult getTSaleDeliverByDeliverCode(String saleDeliverCode ,boolean isOneInsert) throws Exception{
		//查询订单执行情况
		TQueryExecutingDto dto = new TQueryExecutingDto();
		List<TSearchItem> items = new ArrayList<TSearchItem>();
		TSearchItem item = new TSearchItem();
		item.setWhereName("SaleDeliveryDTO.Code");
		item.setBeginValue(saleDeliverCode);
		items.add(item);
		dto.setQueryParam(items);
		String data = JSON.toJSONString(dto);
		System.out.println(data);
		OpenAPI api = new OpenAPI(ConfigBeanAdapter.t_srvUrl,ConfigBeanAdapter.t_appKey,ConfigBeanAdapter.t_appSecret,ConfigBeanAdapter.t_user,ConfigBeanAdapter.t_pwd,ConfigBeanAdapter.t_account);	
		if(isOneInsert){
			System.out.println("开始登录！");
			api.getTLogin();
		}
		String r = api.getData("saleOrder/QueryExecuting",data);
		if(Common.isNotEmpty(r)){
			TSaleDeliverResult result = JSON.parseObject(r, TSaleDeliverResult.class);
			System.out.println(result);
			System.out.println(r);
			return result;
		}
		if(isOneInsert){
			System.out.println("正在退出！");
			api.Logout(api.Access_Token);
		}
		return null;
	}
	/**
	 * @Title: getTSaleDeliverByExternalCode
	 * @Description 根据企商云订单号查询T+销货单执行情况
	 * @param externalCode 企商云订单号
	 * @date 2017年4月17日下午14:01:35
	 * @author LeiJia  
	 * @return TSaleDeliverResult    返回类型
	 */
	public static TSaleDeliverResult getTSaleDeliverByExternalCode(String orderId ,boolean isOneInsert) throws Exception{
		//查询订单执行情况
		TQueryExecutingDto dto = new TQueryExecutingDto();
		List<TSearchItem> items = new ArrayList<TSearchItem>();
		TSearchItem item = new TSearchItem();
		item.setWhereName("SaleDelivery.ExternalCode");
		item.setBeginValue(orderId);
		items.add(item);
		dto.setQueryParam(items);
		String data = JSON.toJSONString(dto);
		System.out.println(data);
		OpenAPI api = new OpenAPI(ConfigBeanAdapter.t_srvUrl,ConfigBeanAdapter.t_appKey,ConfigBeanAdapter.t_appSecret,ConfigBeanAdapter.t_user,ConfigBeanAdapter.t_pwd,ConfigBeanAdapter.t_account);
		if(isOneInsert){
			System.out.println("开始登录！");
			api.getTLogin();
		}
		String r = api.getData("saleOrder/QueryExecuting",data);
		if(Common.isNotEmpty(r)){
			TSaleDeliverResult result = JSON.parseObject(r, TSaleDeliverResult.class);
			System.out.println(result);
			System.out.println(r);
			return result;
		}
		if(isOneInsert){
			System.out.println("正在退出！");
			api.Logout(api.Access_Token);
		}
		return null;
	}
	/**
	 * @Title: insertOrderToTSaleOrder
	 * @Description 向T+系统添加一条销售订单
	 * @param dto 销售订单对象
	 * @date 2017年4月17日下午17:27:35
	 * @author LeiJia  
	 * @return TSaleOrderInsertResult   返回类型
	 */
	public static  TSaleOrderInsertResult insertOrderToTSaleOrder(TSaleOrderInsertDto dto,boolean isOneInsert) throws Exception{
		String data = JSON.toJSONString(dto);
		System.out.println(data);
		OpenAPI api = new OpenAPI(ConfigBeanAdapter.t_srvUrl,ConfigBeanAdapter.t_appKey,ConfigBeanAdapter.t_appSecret,ConfigBeanAdapter.t_user,ConfigBeanAdapter.t_pwd,ConfigBeanAdapter.t_account);
		
		if(isOneInsert){
			System.out.println("开始登录！");
			api.getTLogin();
		}
		String r = api.getData("saleOrder/Create",data);
		if(Common.isNotEmpty(r)){
			TSaleOrderInsertResult result =  JSON.parseObject(r, TSaleOrderInsertResult.class);
			System.out.println(r);
			return result;
		}
		if(isOneInsert){
		   System.out.println("正在退出！");
		   api.Logout(api.Access_Token);
		}	
		return null;
	}
	/**
	 * @Title: insertOrderBatchToTSaleOrder
	 * @Description 向T+系统批量新增销售订单
	 * @param dtos 销售订单对象列表
	 * @date 2017年4月17日下午17:27:35
	 * @author LeiJia  
	 * @return TSaleOrderBatchInsertResult   返回类型
	 */
	public static TSaleOrderBatchInsertResult insertOrderBatchToTSaleOrder(List<TSaleOrderInsertDto> dtos) throws Exception{
		String data = JSON.toJSONString(dtos);
		OpenAPI api = new OpenAPI(ConfigBeanAdapter.t_srvUrl,ConfigBeanAdapter.t_appKey,ConfigBeanAdapter.t_appSecret,ConfigBeanAdapter.t_user,ConfigBeanAdapter.t_pwd,ConfigBeanAdapter.t_account);
		System.out.println("开始登录！");
		api.getTLogin();
		String r = api.getData("saleOrder/CreateBatch", data);
		if(Common.isNotEmpty(r)){
			TSaleOrderBatchInsertResult result =  JSON.parseObject(r, TSaleOrderBatchInsertResult.class);
			System.out.println(r);
			return result;
		}
		System.out.println("正在退出！");
		api.Logout(api.Access_Token);
		return null;
	}


	public static void main(String[] args) throws Exception {
		//String data = "{\"queryParam\": [{\"WhereName\":\"SaleOrder.Code\",BeginValue\":\"SO-2017-03-0001\"}]}";
		//查询订单执行情况
		TQueryExecutingDto dto = new TQueryExecutingDto();
		List<TSearchItem> items = new ArrayList<TSearchItem>();
		TSearchItem item = new TSearchItem();
		item.setWhereName("SaleOrder.Code");
		item.setBeginValue("SO-2017-03-0001");
		items.add(item);
		dto.setQueryParam(items);
		String data = JSON.toJSONString(dto);
		System.out.println(data);
	//	OpenAPI api = new OpenAPI("http://exp.tpluscloud.com/tplus/view/api/v1/","a4e5cec6-7070-4858-a802-57e13adbd699","bjc4b5","demo1","","099");
		OpenAPI api = new OpenAPI();
		System.out.println("开始登录！");
		api.getTLogin();
		String r = api.getData("saleOrder/QueryExecuting",data);
		if(Common.isNotEmpty(r)){
			TSaleOrderResult result = JSON.parseObject(r, TSaleOrderResult.class);
			System.out.println(result);
			System.out.println(r);
		}
		//String s = getData("period/Query",data);
	//	System.out.println(s);
		
		
		//向T+新增一个订单
		TSaleOrderInsertDto dd = new TSaleOrderInsertDto();
		TSaleOrderDto saleOrderDto = new TSaleOrderDto();
		saleOrderDto.setVoucherDate("2017-03-10");
		saleOrderDto.setAddress("郑东新区绿地新都会");
		saleOrderDto.setContactPhone("18737184102");
		TCustomer customer = new TCustomer();
		customer.setCode("533");
		saleOrderDto.setCustomer(customer);
		Object[] dynamicPropertyKeys = {"isautoaudit"};
		saleOrderDto.setDynamicPropertyKeys(dynamicPropertyKeys);
		Object[] dynamicPropertyValues = {true};
		saleOrderDto.setDynamicPropertyValues(dynamicPropertyValues);
		saleOrderDto.setExternalCode("20170330084847590000");
		saleOrderDto.setLinkMan("雷佳");
		saleOrderDto.setMemo("测试OpenAPI");
		List<TSaleOrderDetails> saleOrderDetails = new ArrayList<TSaleOrderDetails>();
		TSaleOrderDetails orderDetail = new TSaleOrderDetails();

		Object[] dynamicPropertyKeys_ = {"priuserdefnvc1","priuserdefdecm1"};
		Object[] dynamicPropertyValues_ = {"sn001","123"};
		orderDetail.setDynamicPropertyKeys(dynamicPropertyKeys_);
		orderDetail.setDynamicPropertyValues(dynamicPropertyValues_);
		orderDetail.setOrigPrice(new BigDecimal(116.4));
		orderDetail.setOrigTaxAmount(new BigDecimal(116.4));
		orderDetail.setQuantity(7);
		TInventory tInventory = new TInventory();
		tInventory.setCode("636-HQ");
		orderDetail.setInventory(tInventory);
		TUnit unit =new TUnit();
		orderDetail.setUnit(unit);
		saleOrderDetails.add(orderDetail);
		saleOrderDto.setSaleOrderDetails(saleOrderDetails);
		dd.setDto(saleOrderDto);
		
		String saleOrderInsertDtoStr = JSON.toJSONString(dd);
		String RR = api.getData("saleOrder/Create",saleOrderInsertDtoStr);
		TSaleOrderInsertResult tSaleOrderInsertResult = null;;
		if(Common.isNotEmpty(RR)){
			tSaleOrderInsertResult = JSON.parseObject(RR, TSaleOrderInsertResult.class);
		}
		System.out.println(RR +"\n" +tSaleOrderInsertResult.getCode());
		TSaleOrderResult result = getTSaleOrderByExternalCode("20170330084847590000",true);
		System.out.print(result);
		

		System.out.println("正在退出！");

		api.Logout(api.Access_Token);
	}

}
